package config

import (
	e "db_ping/err"
	"gopkg.in/yaml.v2"
	"io/ioutil"
)

//Config   系统配置配置
type Config struct {
	Yc struct {
		Task CronConfig  `yaml:"task"`
		Db   []SqlConfig `yaml:"db`
		Mail MailConfig  `yaml:"mail`
	} `yaml:"yc"`
}

//type DbConfig struct {
//	Sql  SqlConfig `yaml:"db"`
//	Task  CronConfig `yaml:"task"`
//}

//数据库配置
type SqlConfig struct {
	Drivername   string `yaml:"drivername"`
	Current      string `yaml:"current"`
	TaskName     string `yaml:"taskname"`
	Url          string `yaml:"url"`
	Username     string `yaml:"username"`
	Password     string `yaml:"password"`
	UrlBack      string `yaml:"url-back"`
	UsernameBack string `yaml:"username-back"`
	PasswordBack string `yaml:"password-back"`
	SshMaster    SSH    `yaml:"ssh-master"`
	SshBack      SSH    `yaml:"ssh-back"`
}

//邮件配置
type MailConfig struct {
	ServerHost     string `yaml:"server-host"`
	ServerPost     int    `yaml:"server-post"`
	FromEmail      string `yaml:"from-email"`
	FromPasswd     string `yaml:"from-passwd"`
	FromEmailAlias string `yaml:" from-email-alias"`
	Toers          string `yaml:"toers"`
	Ccers          string `yaml:"ccers"`
}

type SSH struct {
	UserName    string `yaml:"username"`
	SSHLineType string `yaml:"ssh-line-type"`
	PassWord    string `yaml:"password"`
	SSHKeyPath  string `yaml:"ssh-key-path"`
	Host        string `yaml:"host"`
	Port        int    `yaml:"port"`
}

//定时器配置
type CronConfig struct {
	Cron string `yaml:"cron"`
}

func GetConfig(fileName string) *Config {
	var setting Config
	config, err := ioutil.ReadFile(fileName)
	e.Err(err)
	err = yaml.Unmarshal(config, &setting)
	e.Err(err)
	return &setting
}

func GetSqlConfig(fileName string) *[]SqlConfig {
	var setting Config
	config, err := ioutil.ReadFile(fileName)
	e.Err(err)
	err = yaml.Unmarshal(config, &setting)
	e.Err(err)
	return &setting.Yc.Db
}

func GetCronConfig(fileName string) *CronConfig {
	var setting Config
	config, err := ioutil.ReadFile(fileName)
	e.Err(err)
	err = yaml.Unmarshal(config, &setting)
	e.Err(err)
	return &setting.Yc.Task
}
